package common
{
	import asset.commonAlertViewAsset;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import stages.StageReflection;
	
	import utils.DisplayUtil;
	
	public class AlertView extends commonAlertViewAsset
	{
		private var _message:String;
		private var _txtText:TextField;
		
		public function AlertView(message:String)
		{
			_message=message;
			
			_txtText=new TextField();
			_txtText.autoSize=TextFieldAutoSize.CENTER;
			_txtText.width=300;
			_txtText.selectable=false;
			_txtText.mouseEnabled=false;
			_txtText.multiline=true;
			_txtText.wordWrap=true;
			_txtText.text=_message;
			_txtText.width=(_txtText.textWidth+10)>500 ? 500 : (_txtText.textWidth+10);
			_txtText.height=_txtText.textHeight+4;
			
			var w:int=_txtText.width<=300 ? 300 : _txtText.width;
			graphics.clear();
			graphics.beginFill(0xd6d6d6);
			graphics.lineStyle(1, 0x646464)
			graphics.moveTo(0, 0);
			graphics.lineTo(0, _txtText.height+60);
			graphics.lineTo(w+20, _txtText.height+60);
			graphics.lineTo(w+20, 0);
			graphics.lineTo(0, 0);
			graphics.endFill();
			
			_txtText.x=(width-_txtText.width)/2;
			_txtText.y=10;
			addChild(_txtText);
			
			btnComplete.buttonMode=btnCancel.buttonMode=true;
			btnComplete.x=(width-300)/2+(300/3)-btnComplete.width/2;
			btnCancel.x=(width-300)/2+(300/3)*2-btnCancel.width/2;
			btnComplete.y=height-btnComplete.height-15;
			btnCancel.y=height-btnCancel.height-15;
			btnComplete.txtText.mouseEnabled=btnCancel.txtText.mouseEnabled=false;
			btnComplete.addEventListener(MouseEvent.CLICK, __onBtnClick);
			btnCancel.addEventListener(MouseEvent.CLICK, __onBtnClick);
		}
		
		private function __onBtnClick(evt:MouseEvent):void
		{
			switch(evt.currentTarget)
			{
				case btnComplete:
					dispatchEvent(new Event(Event.COMPLETE));
					break;
				case btnCancel:
					dispatchEvent(new Event(Event.CANCEL));
					break;
			}
		}
		
		public function show():void
		{
			x=(StageReflection.stageWidth-width)/2;
			y=(StageReflection.stageHeight-height)/2;
			StageReflection.stage.addChild(this);
		}
		
		public function dispose():void
		{
			btnComplete.removeEventListener(MouseEvent.CLICK, __onBtnClick);
			btnCancel.removeEventListener(MouseEvent.CLICK, __onBtnClick);
			
			graphics.clear();
			DisplayUtil.dispose(_txtText);
			_txtText=null;
			if(parent)parent.removeChild(this);
		}
	}
}